
package simuri.apriori;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import simuri.uridb.DbConnection;
import simuri.utils.Configuration;

/**
 * Simply count the number of edges to a node to determine how "popular" it is
 * and uses the log of this count as a-priori score. Log is used because we assume
 * that we have a long-tail distribution of the number of edges for each node.
 * @author fomy
 */

public class DbEdgeCount implements AprioriFunction {
    
    static Logger logger = Logger.getLogger("AEON");
    
    /**
     * Computes the a-priori score based on a single uri
     * @param uri URI of the resource
     * @return A-priori score
     */    
    @Override
   public double getDbAprioriScore1(String uri) {
       
    double nbedges = 0.0;
    Connection con = DbConnection.getConnection();
    try {
        Statement stmt = con.createStatement();
        String sql = "SELECT ingoing_edges FROM uri_aprioriscores WHERE uri_name='" + uri + "'";
        ResultSet rset = stmt.executeQuery(sql);
        while (rset.next()) {
            nbedges = rset.getDouble("ingoing_edges");
        }

        stmt.close();
        con.close();
        } catch (SQLException ex) {
            System.out.println("SQL Fehler " + ex.getMessage());
            ex.printStackTrace(System.out);
        }
        return nbedges;
    }
  

    /** Batch version
     * @param uris List of URIs
     * @return Map of uris to scores
     */


    @Override
    public HashMap<String, Double> getDbAprioriScore1(TreeSet<String> uris) {
        HashMap<String, Double> result = new HashMap<>();
        for (String uri : uris) {
            result.put(uri, getDbAprioriScore1(uri));
        }
        return result;
    }

    @Override
    public HashMap<String, Double> getDbAprioriScore1(TreeSet<String> uris, Configuration c) {
        return getDbAprioriScore1(uris);
    }
    
}
